<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>角色管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8"/>
    <link rel="stylesheet" href="../../css/font.css">
    <link rel="stylesheet" href="../../css/xadmin.css">
    <!--<link rel="stylesheet" href="../../css/theme2316.min.css">-->
    <link rel="stylesheet" href="../../lib/layui/css/layui.css" media="all">
    <script src="../../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../js/xadmin.js"></script>

    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>



<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <div class="am-cf am-padding am-padding-bottom-0">

                        <form class="layui-form layui-col-space5">
                            <div class="x-nav">
          <span class="layui-breadcrumb">
            <a href="">首页</a>

            <a>
              <cite>角色管理/Role management</cite></a>
          </span>
                                <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
                                   onclick="location.reload()" title="刷新">
                                    <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
                            </div>
                            <br>
                            <div class="layui-inline layui-show-xs-block">
                                <input type="text" ID="roleName" placeholder="" autocomplete="off"
                                       class="layui-input">
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <button class="layui-btn" lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i>
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
                <div class="layui-card-header demoTable">
                    <button class="layui-btn" onclick="xadmin.open('新增角色','./6.11-add.html',600,400)"><i
                            class="layui-icon"></i>添加
                    </button>
                    <button class="layui-btn layui-btn-danger" data-type="getCheckData"><i class="layui-icon"></i>批量删除
                    </button>
                </div>

                <div class="layui-card-body layui-table-body layui-table-main">
                    <table class="layui-table layui-form" id="demo" lay-filter="demo">
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    layui.use(['laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element', 'slider', 'form'], function () {
        var laydate = layui.laydate //日期
            , laypage = layui.laypage //分页
            , layer = layui.layer //弹层
            , table = layui.table //表格
            , carousel = layui.carousel //轮播
            , upload = layui.upload //上传
            , element = layui.element //元素操作
            , slider = layui.slider //滑块
            , table = layui.table
            , form = layui.form


        //监听提交（查询用法）
        form.on('submit(sreach)', function () {
            var roleName = $('#roleName').val();
            $.ajax({
                url: '/JT90/user_type/selectUserTypeByNameOrPinyinInitials',//地址
                dataType: 'json',//数据类型
                data: {"roleName": roleName},
                type: 'GET',//类型
                contentType: 'application/json;charset=UTF-8',
                //请求成功
                success: function () {
                    //分页
                    table.render({
                        elem: '#demo'
                        , id: 'test'
                        , height: 296
                        , url: '/JT90/user_type/selectUserTypeByNameOrPinyinInitials?roleName=' + roleName //数据接口
                        , page: {
                            count: 100
                            , layout: ['limit', 'count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
                            , groups: 5 //只显示 1 个连续页码
                            , first: '首页'//不显示首页
                            , last: '尾页' //不显示尾页
                            , limit: 7
                            , limits: [5, 10, 15, 20]
                            , theme: '#44cc33'
                            , jump: function (obj) {
                                console.log(obj)
                            }
                        }//开启分页
                        , cols: [[ //表头
                            {type: 'checkbox', width: '16.7%'}
                            , {field: 'roleId', title: '角色ID', width: '16.7%'}
                            , {field: 'roleName', title: '角色名称', width: '16.7%'}
                            , {field: 'roleExplain', title: '说明', width: '16.7%'}
                            , {
                                field: 'right', title: '状态', width: '16.7%', templet: function (d) {
                                    var state = "";
                                    if (d.roleState == "15") {
                                        state = "<input type='checkbox' value='" + d.usageId + "' id='status' lay-filter='stat' checked='checked' name='status'  lay-skin='switch' lay-text='启用|停用' >";
                                    } else {
                                        state = "<input type='checkbox' value='" + d.usageId + "' id='status' lay-filter='stat'  name='status'  lay-skin='switch' lay-text='启用|停用' >";
                                    }
                                    return state;
                                }
                            }
                            , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: '16.7%'}
                        ]]
                        , even: true
                    });
                }
            });

            return false;
        });
        //分页
        table.render({
            elem: '#demo'
            , id: 'test'
            , height: 286
            , url: '/JT90/user_type/selectUserTypeList' //数据接口
            , page: {
                count: 100
                , layout: ['limit', 'count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
                , groups: 5 //只显示 1 个连续页码
                , first: '首页'//不显示首页
                , last: '尾页' //不显示尾页
                , limit: 5
                , limits: [5, 10, 15, 20]
                , theme: '#44cc33'
                , jump: function (obj) {
                    console.log(obj)
                }
            }//开启分页
            , cols: [[ //表头
                {type: 'checkbox', width: '16.7%'}
                , {field: 'roleId', title: '角色ID', width: '16.7%'}
                , {field: 'roleName', title: '角色名称', width: '16.7%'}
                , {field: 'roleExplain', title: '说明', width: '16.7%'}
                , {
                    field: 'right', title: '状态', width: '16.7%', templet: function (d) {
                        var state = "";
                        if (d.roleState == "15") {
                            state = "<input type='checkbox' value='" + d.roleId + "' id='status' lay-filter='stat' checked='checked' name='status'  lay-skin='switch' lay-text='启用|停用' >";
                        } else {
                            state = "<input type='checkbox' value='" + d.roleId + "' id='status' lay-filter='stat'  name='status'  lay-skin='switch' lay-text='启用|停用' >";
                        }
                        return state;
                    }
                }
                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: '16.7%'}
            ]]
            , even: true
        });

        //监听工具条
        table.on('tool(demo)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
            if (layEvent === 'del') {
                layer.confirm('确认删除这条数据吗？', function (index) {
                    var roleId = data.roleId;//获取选中行的对象的id值
                    //发异步删除数据
                    $.ajax({
                        url: '/JT90/user_type/deleteUserTypeById',//地址
                        dataType: 'json',//数据类型
                        data: {"roleId": roleId,},
                        type: 'GET',//类型
                        contentType: 'application/json;charset=UTF-8',
                        //请求成功
                        success: function (map) {
                            if (map.msg) {
                                $(obj).parents("tr").remove();
                                if (map.msg) {
                                    layer.msg('已删除!', {
                                        icon: 1,
                                        time: 1000
                                    });
                                }

                            }
                        }
                    });
                    /*用户-删除*/
                    obj.del();
                    layer.close(index);

                });
            } else if (obj.event === 'edit') {
                var data = obj.data; //获得当前行数据
                /* var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）*/
                var roleId = data.roleId;
                var roleName = data.roleName;//获取选中行的对象的id值
                var roleExplain = data.roleExplain;
                //编辑弹窗事件
                layer.open({
                    type: 2,
                    area: ['600px', '400px'],
                    title: '修改角色',
                    content: 'http://122.51.140.205:8892/JT90/index/6BasicArchives/6.11-edit.html?roleId=' + roleId + '&roleName=' + roleName + '&roleExplain=' + roleExplain,
                    success: function () {
                        var body = layer.getChildFrame('body');//获取弹窗页面
                        body.find('#roleId').val(roleId);
                        body.find('#roleName').val(roleName);
                        body.find('#roleExplain').val(roleExplain);
                    }

                });

            }
        });
        //监听状态开关
        form.on('switch(stat)', function (obj) {
            var roleId = this.value, boolean = obj.elem.checked;
            if (boolean) {
                var roleState = 15;
            } else {
                roleState = 16;
            }
            console.log(roleId + "---" + roleState);
            $.ajax({
                url: '/JT90/user_type/updateUserTypeStateById',//地址
                dataType: 'json',//数据类型
                data: {"roleId": roleId, "roleState": roleState},
                type: 'GET',//类型
                contentType: 'application/json;charset=UTF-8',
                //请求成功
                success: function (map) {
                    if (map.msg) {
                        if (map.data.roleState == 15) {
                            layer.alert("已启用", {
                                icon: 6
                            })
                        } else {
                            layer.alert("已停用", {
                                icon: 6
                            })
                        }
                        console.log(map.data);
                    }
                    return false;
                },
                //失败/超时
                error: function () {
                    alert('請求超時');
                    //alert(errorThrown);
                }
            });
        });


        //监听表格复选框选择
        table.on('checkbox(demo)', function (obj) {
            console.log(obj.data)
        });
        //批量删除
        var $ = layui.$, active = {
            getCheckData: function (obj) { //获取选中数据
                var checkStatus = table.checkStatus('test')
                    , data = checkStatus.data;
                if (data == 0) {
                    layer.msg('至少选择一个', {icon: 2});

                    return;
                }
                var checkList = "";
                if (data.length > 0) {
                    for (var i = 0; i < data.length; i++) {
                        checkList += data[i].roleId + ",";
                    }
                    console.log(checkList);
                }
                layer.confirm('确认要删除吗？', function () {
                    $.ajax({
                        url: '/JT90/user_type/deleteUserTypesById',//地址
                        dataType: 'json',//数据类型
                        data: {"checkList": checkList},
                        type: 'GET',//类型
                        contentType: 'application/json;charset=UTF-8',
                        //请求成功
                        success: function (map) {
                            if (map.msg) {
                                if (map.msg) {
                                    layer.msg('已删除!', {
                                        icon: 1,
                                        time: 1500,
                                        end: function () {
                                            location.reload();
                                        }
                                    });
                                }

                            }

                        }
                    });

                });

            }
        };

        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });


    });
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
</html>